Conditions | 1 |
Paths | 1 |
Total Lines | 148 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* |
||
24 | function init(plot){ |
||
25 | var orderedBarSeries; |
||
26 | var nbOfBarsToOrder; |
||
27 | var borderWidth; |
||
28 | var borderWidthInXabsWidth; |
||
29 | var pixelInXWidthEquivalent = 1; |
||
30 | var isHorizontal = false; |
||
31 | |||
32 | /* |
||
33 | * This method add shift to x values |
||
34 | */ |
||
35 | function reOrderBars(plot, serie, datapoints){ |
||
36 | var shiftedPoints = null; |
||
37 | |||
38 | if(serieNeedToBeReordered(serie)){ |
||
39 | checkIfGraphIsHorizontal(serie); |
||
40 | calculPixel2XWidthConvert(plot); |
||
41 | retrieveBarSeries(plot); |
||
42 | calculBorderAndBarWidth(serie); |
||
43 | |||
44 | if(nbOfBarsToOrder >= 2){ |
||
45 | var position = findPosition(serie); |
||
46 | var decallage = 0; |
||
47 | |||
48 | var centerBarShift = calculCenterBarShift(); |
||
49 | |||
50 | if (isBarAtLeftOfCenter(position)){ |
||
51 | decallage = -1*(sumWidth(orderedBarSeries,position-1,Math.floor(nbOfBarsToOrder / 2)-1)) - centerBarShift; |
||
52 | }else{ |
||
53 | decallage = sumWidth(orderedBarSeries,Math.ceil(nbOfBarsToOrder / 2),position-2) + centerBarShift + borderWidthInXabsWidth*2; |
||
54 | } |
||
55 | |||
56 | shiftedPoints = shiftPoints(datapoints,serie,decallage); |
||
57 | datapoints.points = shiftedPoints; |
||
58 | } |
||
59 | } |
||
60 | return shiftedPoints; |
||
61 | } |
||
62 | |||
63 | function serieNeedToBeReordered(serie){ |
||
64 | return serie.bars != null |
||
65 | && serie.bars.show |
||
66 | && serie.bars.order != null; |
||
67 | } |
||
68 | |||
69 | function calculPixel2XWidthConvert(plot){ |
||
70 | var gridDimSize = isHorizontal ? plot.getPlaceholder().innerHeight() : plot.getPlaceholder().innerWidth(); |
||
71 | var minMaxValues = isHorizontal ? getAxeMinMaxValues(plot.getData(),1) : getAxeMinMaxValues(plot.getData(),0); |
||
72 | var AxeSize = minMaxValues[1] - minMaxValues[0]; |
||
73 | pixelInXWidthEquivalent = AxeSize / gridDimSize; |
||
74 | } |
||
75 | |||
76 | function getAxeMinMaxValues(series,AxeIdx){ |
||
77 | var minMaxValues = new Array(); |
||
|
|||
78 | for(var i = 0; i < series.length; i++){ |
||
79 | minMaxValues[0] = series[i].data[0][AxeIdx]; |
||
80 | minMaxValues[1] = series[i].data[series[i].data.length - 1][AxeIdx]; |
||
81 | } |
||
82 | return minMaxValues; |
||
83 | } |
||
84 | |||
85 | function retrieveBarSeries(plot){ |
||
86 | orderedBarSeries = findOthersBarsToReOrders(plot.getData()); |
||
87 | nbOfBarsToOrder = orderedBarSeries.length; |
||
88 | } |
||
89 | |||
90 | function findOthersBarsToReOrders(series){ |
||
91 | var retSeries = new Array(); |
||
92 | |||
93 | for(var i = 0; i < series.length; i++){ |
||
94 | if(series[i].bars.order != null && series[i].bars.show){ |
||
95 | retSeries.push(series[i]); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return retSeries.sort(sortByOrder); |
||
100 | } |
||
101 | |||
102 | function sortByOrder(serie1,serie2){ |
||
103 | var x = serie1.bars.order; |
||
104 | var y = serie2.bars.order; |
||
105 | return ((x < y) ? -1 : ((x > y) ? 1 : 0)); |
||
106 | } |
||
107 | |||
108 | function calculBorderAndBarWidth(serie){ |
||
109 | borderWidth = serie.bars.lineWidth ? serie.bars.lineWidth : 2; |
||
110 | borderWidthInXabsWidth = borderWidth * pixelInXWidthEquivalent; |
||
111 | } |
||
112 | |||
113 | function checkIfGraphIsHorizontal(serie){ |
||
114 | if(serie.bars.horizontal){ |
||
115 | isHorizontal = true; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | function findPosition(serie){ |
||
120 | var pos = 0 |
||
121 | for (var i = 0; i < orderedBarSeries.length; ++i) { |
||
122 | if (serie == orderedBarSeries[i]){ |
||
123 | pos = i; |
||
124 | break; |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return pos+1; |
||
129 | } |
||
130 | |||
131 | function calculCenterBarShift(){ |
||
132 | var width = 0; |
||
133 | |||
134 | if(nbOfBarsToOrder%2 != 0) |
||
135 | width = (orderedBarSeries[Math.ceil(nbOfBarsToOrder / 2)].bars.barWidth)/2; |
||
136 | |||
137 | return width; |
||
138 | } |
||
139 | |||
140 | function isBarAtLeftOfCenter(position){ |
||
141 | return position <= Math.ceil(nbOfBarsToOrder / 2); |
||
142 | } |
||
143 | |||
144 | function sumWidth(series,start,end){ |
||
145 | var totalWidth = 0; |
||
146 | |||
147 | for(var i = start; i <= end; i++){ |
||
148 | totalWidth += series[i].bars.barWidth+borderWidthInXabsWidth*2; |
||
149 | } |
||
150 | |||
151 | return totalWidth; |
||
152 | } |
||
153 | |||
154 | function shiftPoints(datapoints,serie,dx){ |
||
155 | var ps = datapoints.pointsize; |
||
156 | var points = datapoints.points; |
||
157 | var j = 0; |
||
158 | for(var i = isHorizontal ? 1 : 0;i < points.length; i += ps){ |
||
159 | points[i] += dx; |
||
160 | //Adding the new x value in the serie to be abble to display the right tooltip value, |
||
161 | //using the index 3 to not overide the third index. |
||
162 | serie.data[j][3] = points[i]; |
||
163 | j++; |
||
164 | } |
||
165 | |||
166 | return points; |
||
167 | } |
||
168 | |||
169 | plot.hooks.processDatapoints.push(reOrderBars); |
||
170 | |||
171 | } |
||
172 | |||
188 |